package com.meizhuo.listview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatImageView;

import com.bumptech.glide.Glide;
import com.meizhuo.listview.DataSaveUtil.ListDataSave;
import com.meizhuo.listview.api.HttpClient;
import com.meizhuo.listview.network.AddFriend_Interface;
import com.meizhuo.listview.network.GetUsername_Interface;
import com.meizhuo.listview.network_data.recv_data.Register_body;
import com.meizhuo.listview.network_data.send_data.Register;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddFriendActivity extends Activity implements View.OnClickListener {

    private EditText searchView;
    private Button search;
    private AppCompatImageView addFriend_pic;
    private TextView addFriend_title;
    private Button addFriend_btn;
    private Register data;
    private HttpClient<Register_body> httpClient = HttpClient.getInstance();
    private int id;
    private TextView addFriend_username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addfriend);
        id = ListDataSave.getInstance(getApplicationContext(), "Userdata").getData("user").getId();
        data = null;
        initView();

    }


    private void initView() {
        searchView = (EditText) findViewById(R.id.searchView);

        search = (Button) findViewById(R.id.search);
        search.setOnClickListener(this);
        addFriend_pic = (AppCompatImageView) findViewById(R.id.addFriend_pic);
        addFriend_pic.setOnClickListener(this);
        addFriend_title = (TextView) findViewById(R.id.addFriend_title);
        addFriend_title.setOnClickListener(this);
        addFriend_btn = (Button) findViewById(R.id.addFriend_btn);
        addFriend_btn.setOnClickListener(this);
        addFriend_username = (TextView) findViewById(R.id.addFriend_username);
        addFriend_username.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.search:
                String username = searchView.getText().toString();

                System.out.println(username);

                // 请求体传入接收的结果
                GetUsername_Interface request = httpClient.getHttpClient().create(GetUsername_Interface.class);

                //对 发送请求 进行封装
                Call<Register_body> call = request.getCall(username);

                httpClient.start(call, new Callback<Register_body>() {
                    //请求成功时回调
                    @Override
                    public void onResponse(Call<Register_body> call, final Response<Register_body> response) {

                        if (response.body() == null) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(AddFriendActivity.this, "请求出错", Toast.LENGTH_SHORT).show();
                                }
                            });
                            return;
                        }

                        // 等于200 说明请求成功，跳转页面
                        if (response.body().getStatus() == 200) {

                            data = response.body().getData();

                            addFriend_username.setText("账号：  "+data.getUsername());
                            addFriend_title.setText("昵称：   "+data.getNickname());
                            Glide.with(addFriend_pic.getContext()).load(data.getAvatar()).into(addFriend_pic);

                        } else {

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(AddFriendActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            });

                        }

                    }

                    //请求失败时候的回调
                    @Override
                    public void onFailure(Call<Register_body> call, Throwable throwable) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(AddFriendActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });

                break;
            case R.id.addFriend_btn:
                if (data == null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AddFriendActivity.this, "请先查询用户信息", Toast.LENGTH_SHORT).show();
                        }
                    });
                } else {

                    // 请求体传入接收的结果
                    AddFriend_Interface req = httpClient.getHttpClient().create(AddFriend_Interface.class);

                    //对 发送请求 进行封装
                    Call<Register_body> reqCall = req.getCall(id, data.getId());

                    httpClient.start(reqCall, new Callback<Register_body>() {
                        //请求成功时回调
                        @Override
                        public void onResponse(Call<Register_body> call, final Response<Register_body> response) {

                            if (response.body() == null) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(AddFriendActivity.this, "请求出错", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                return;
                            }

                            // 等于200 说明请求成功，跳转页面
                            if (response.body().getStatus() == 200) {

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(AddFriendActivity.this, "添加好友成功", Toast.LENGTH_SHORT).show();
                                    }
                                });

                                Intent intent = new Intent(AddFriendActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();

                            } else {

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(AddFriendActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                                    }
                                });

                            }

                        }

                        //请求失败时候的回调
                        @Override
                        public void onFailure(Call<Register_body> call, Throwable throwable) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(AddFriendActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    });

                }
                break;
        }
    }
}

